<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * ketrampilan.php
 * @author ranuandra
 *
 * 11 Jun 2012 01:16:28
 *
 * 
 */

class Controller_Ketrampilan extends Kohana_Nasmoco
{
	public function action_index()
	{
		
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$ketrampilans = $pelamar->pelamarketrampilan->find_all();
		$view = View::factory('ketrampilan/index');
		$view->datas = $ketrampilans;
		$this->template->container = $view;
	}
	
	public function action_add()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$view = View::factory('ketrampilan/form');
		$view->flag = $pelamar->flag_ketrampilan;
		$view->lama = array('Dibawah 1 Tahun' => 'Dibawah 1 tahun', '1 tahun s/d 2 tahun' => '1 tahun s/d 2 tahun', '3 tahun s/d 5 tahun' => '3 tahun s/d 5 tahun', 'Diatas 5 tahun' => 'Diatas 5 tahun' );
		$view->level = array('Pemula' => 'Pemula', 'Cukup Mahir' => 'Cukup Mahir', 'Mahir' => 'Mahir', 'Sangat Mahir' => 'Sangat Mahir');
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();

			try 
			{
				$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(Request::current()->post('flag_ketrampilan'))
				{
					
					$pelamar->flag_ketrampilan = Request::current()->post('flag_ketrampilan');
					$pelamar->save();
				}
				
				if(isset($_POST['id']))
				{
					$ketrampilan = ORM::factory('pelamarketrampilan', Request::current()->post('id'));
					$ketrampilan->ketrampilan = Request::current()->post('ketrampilan');
					$ketrampilan->lama_pengalaman = Request::current()->post('lama_pengalaman');
					$ketrampilan->level = Request::current()->post('level');
					$ketrampilan->save();
				}
				else 
				{
					$ketrampilan = ORM::factory('pelamarketrampilan');
					$ketrampilan->mstpelamar_id = $pelamar->id;
					$ketrampilan->created = date("Y-m-d H:i:s");
					$ketrampilan->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}
	}
}